package cn.udrm.elasticsearch.web;

import cn.udrm.elasticsearch.fulltext.BuildFileIndex;
import cn.udrm.elasticsearch.service.ExcuteFullTextService;
import lombok.extern.slf4j.Slf4j;
import org.apache.poi.poifs.filesystem.NDocumentInputStream;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

@Slf4j
@Controller
@RequestMapping(value = "/fulltext/")
public class StartFullTxtController {

    @Value("${es.index-root}")
    private String indexRoot;

    @Value("${es.index-Name}")
    private String indexName;

    @Value("${es.doc-Type}")
    private String docType;

    @Value("${fulltext.threads:1}")
    private int THREAD_COUNT;
    @Value("${file.depth:0}")
    private int depth;

    @Value("${fulltext.types}")
    private String fileTypes;

    @Resource
    private ExcuteFullTextService excuteFullTextService;

    @ResponseBody
    @RequestMapping(value = "/addIndex")
    public String addIndex(HttpServletResponse response){
        try {
            indexRoot=new String(indexRoot.getBytes("ISO-8859-1"),"UTF-8");
            excuteFullTextService.start(fileTypes,indexRoot,depth,indexName,docType);
        } catch (Exception e) {
            log.error("创建索引失败！",e);
        }
        return "创建索引结束！";
    }
}
